/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.courses.editor;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import fr.ove.openmath.jome.Jome;

import fr.ove.openmath.courses.editor.events.*;

public class BasicEditor extends Frame {
	TextField entry;
	Jome jome;
	
    private Vector listeners = new Vector();
	
	public BasicEditor() {
		setLayout(new BorderLayout(0,0));
		setTitle("Modify the Current Expression");
		setSize(350,250);
		
		addWindowListener(
		    new WindowAdapter() {
        		public void windowClosing(WindowEvent event) {
            		setVisible(false);
            		dispose();
            	}
        	}
        );
		
		jome = new Jome();
		jome.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		jome.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		jome.setDrawBounds(false);
		add("Center", jome);
		
		entry = new TextField();
		entry.setBounds(0,323,405,24);
		entry.setFont(new Font("Dialog", Font.BOLD, 14));
		add("South", entry);
		
		entry.addTextListener(
        	new TextListener() {
        		public void textValueChanged(TextEvent event) {
        			jome.setLinear(entry.getText());
        		}
        	}
        );
		
		entry.addActionListener(
		    new ActionListener() {
        		public void actionPerformed(ActionEvent event) {
            		setVisible(false);
        			
        			EditorEvent editorEvent = new EditorEvent(BasicEditor.this);
        			editorEvent.setAction("update", jome.getLinear());
        			fireEditorEvent(editorEvent);
        		}
        	}
		);
	}
	
	public void setExpression(String linearExpr) {
	    jome.setLinear(linearExpr);
		entry.setText(linearExpr);
	}
	
    /**
    * Registers a listener of the instance.
    */
    public void addEditorListener(EditorListener editorListener) {
        listeners.addElement(editorListener);
    }
        
    /**
    * Unregisters the specified listener of the instance.
    * @param editorListener the listener to remove.
    */
    public void removeEditorListener(EditorListener editorListener) {
        listeners.removeElement(editorListener);
    }

    /**
    * Fires an event to all the registered listeners of the instance.
    * @param editorEvent the event to fire.
    */
    public void fireEditorEvent(EditorEvent editorEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((EditorListener) listeners.elementAt(i)).consumeEditorEvent(editorEvent);
    }
}

